package com.tools
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class Timing
	{
		private var s:int=10;//计时间隔(毫秒)
		private var t:Timer;
		private var count:int=0;
		private var dict:Object;
		static public var self:Timing=new Timing();
		public function Timing()
		{
			dict={};
			t=new Timer(s);
			t.addEventListener(TimerEvent.TIMER,run);
			t.start();
		}
		/**
		 *添加倒计时监听 
		 * @param key 名字
		 * @param interval 间隔(秒)必需>=0.1
		 * @param callBack 回调方法
		 * @param param 回调参数
		 * 
		 */		
		public function addListen(key:String,interval:Number,callBack:Function,param:Object=null):void{
			if(interval*1000<s)interval=s;
			dict[key]={func:callBack,param:param,time:count,interval:interval*1000};
		}
		/**
		 *删除指定计时 
		 * @param key 名字
		 * 
		 */		
		public function removeListen(key:String):void{
			dict[key]=null;
		}
		private function run(e:TimerEvent):void{
			count++;
			for each(var o:Object in dict){
				if(o!=null&&o.func!=null&&(count-o.time)*s>=o.interval){
					if(o.param!=null)o.func(o.param);
					else o.func();
					o.time=count;
				}
			}
		}
	}
}